package br.com.perkins.myproject.presentation.exception;

import javax.faces.FacesException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.com.perkins.myproject.service.exception.ServiceException;

import com.sun.faces.application.ActionListenerImpl;

public class PresentationExceptionHandler extends ActionListenerImpl {

	@Override
	public void processAction(ActionEvent event) {
		FacesContext context = FacesContext.getCurrentInstance();

		try {
			super.processAction(event);
		} catch (FacesException facesException) {
			ServiceException serviceException = (ServiceException) facesException.getCause().getCause();
			for (int i  : serviceException.getServiceExceptions().keySet()) {
				context.addMessage(null, new FacesMessage(
						FacesMessage.SEVERITY_FATAL, serviceException.getServiceExceptions().get(i) + "(#" + i + ")", null));
			}
		} catch (Exception exception) {
			context.addMessage(null, new FacesMessage(
				FacesMessage.SEVERITY_FATAL, "Unknown Error", null));
		}
	}
}